/* 
* 关于动态图标设置
*/
var manifest = chrome.runtime.getManifest();
var matches_reg = manifest.content_scripts[0].matches.map(m => m.replace(/\*/g, '.*'));;

chrome.tabs.onActivated.addListener((activeInfo) => {
    chrome.tabs.get(activeInfo.tabId, tabHandler);
});

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    if (changeInfo.status == 'complete') {
        tabHandler(tab);
    }
});

function tabHandler(tab) {
    let url = tab.url;
    let isMatched = matches_reg.some(m => new RegExp(m).test(url));
    // console.log('urlMatch:', isMatched, url);
    if (isMatched) {
        chrome.action.setIcon({ path: "../icons/timeline.png" });
        chrome.action.enable();
    } else {
        chrome.action.setIcon({ path: "../icons/timeline_grey.png" });
        chrome.action.disable();
    }
}